package springsecurity_oauth2.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    //在下面的方法中使用了and进行连缀
    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf()
                .disable()

                .authorizeRequests()
                .antMatchers("/oauth/**", "/login/**", "logout/**").permitAll()  //直接放行
                .anyRequest().authenticated()   // 全部都需要认证
                .and()

                .formLogin().permitAll();
    }
}
